package com.ruoyi.manage.service.impl;

import com.ruoyi.manage.service.IManageUserService;
import com.ruoyi.manage.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ManageUserServiceImpl implements IManageUserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void createSmsCode(final String phonenumber) {
        // 1.生成一个6位随机数（验证码）
        String smsCode = (long) (Math.random() * 1000000) + "";
        System.out.println("验证码：" + smsCode);
        // 2.将验证码放入redis  有效期5分钟
        redisTemplate.opsForValue().set(phonenumber, smsCode, 300);
        //3.将短信内容发送给业主
        Map<String, String> map = new HashMap<>();
        map.put("code", smsCode);
        String signName = "取字典数据";
        String templateCode = "取字典数据";
//        SmsUtil.sendSms(phonenumber, signName, templateCode, map);
    }

    @Override
    public boolean checkSmsCode(String phonenumber, String smsCode) {
        String systemCode = (String) redisTemplate.boundHashOps("smsCode").get(phonenumber);
        if (systemCode == null) {
            return false;
        }
        if (!systemCode.equals(smsCode)) {
            return false;
        }

        return true;
    }
}
